<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Document extends SMB_Controller { 
	
	public function __construct() {
		parent::__construct();
		$this->load->library('file');
	}
	
	public function index() {
		$this->_get_userinfo();	
		$this->load->helper('table');
	
		$this->_load_common_header();
		$this->_load_common_navigation();
		$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
		$params = array(
			'files' => $this->file->get_files($this->userinfo['username']),
			'downloadstatus' => $this->session->flashdata('downloadstatus'),
			'notfoundfile' => $this->session->flashdata('notfoundfile'),
			'uploadstatus' => $this->session->flashdata('uploadstatus'),
			'deletestatus' => $this->session->flashdata('deletestatus'),
			'deletefile' => $this->session->flashdata('deletefile'),
			'pager' => get_pager(4)
		);
		$this->load->view('mahasiswa/document_list', $params);
		$this->_load_common_footer();
	}
	
	public function download($id = NULL) {
		$this->_get_userinfo();
		
		if ($id == NULL) {
			redirect('mahasiswa/profile/document', 'location');
			exit;
		}
		$file = $this->file->get_category_by_username_and_id($this->userinfo['username'], $id);
		if ($file == NULL) {
			redirect('mahasiswa/profile/document', 'location');
			exit;
		}
		
		$this->load->helper('download');
		$filename = do_hash($this->userinfo['username'].' - '.$file->name.'.pdf', 'sha1');
		$pathfile = 'files/studentdocument/'.$filename;
		if (file_exists($pathfile)) {
			$name = $this->userinfo['username'].' - '.$file->name.'.pdf';
			$data = file_get_contents($pathfile);
			force_download($name, $data);
			$this->session->set_flashdata('downloadstatus', 'success');
			redirect('mahasiswa/profile/document', 'location');
		} else {
			$this->session->set_flashdata('downloadstatus', 'failed');
			$this->session->set_flashdata('notfoundfile', $file->name);
			redirect('mahasiswa/profile/document', 'location');
		}
	}
	
	public function upload() {
		$this->_get_userinfo();
		
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
		$status = $this->input->post('upload');
		if ($this->form_validation->run('upload_document') == FALSE) {
			if ($status == 'Batal') {
				redirect('mahasiswa/profile/document', 'location');
				exit;
			} else {
				$params = array(
					'categories' => $this->file->get_categories(),
					'result' => ''
				);
				
				$this->_load_common_header();
				$this->_load_common_navigation();
				$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
				$this->load->view('mahasiswa/document_upload', $params);
				$this->_load_common_footer();
			}
		} else {
			if ($status == 'Upload') {
				$id = $this->input->post('id');
				$details = $this->input->post('details');
				if (trim($details) == '') {
					$details = '-';
				}
				$file = $this->file->get_category_by_id($id);
				$config['upload_path'] = 'files/studentdocument/';
				$config['allowed_types'] = 'pdf';
				$config['max_size'] = MAX_DOC_SIZE;
				$config['overwrite'] = TRUE;
				$config['file_name'] = do_hash($this->userinfo['username'].' - '.$file->name.'.pdf', 'sha1');
				$this->load->library('upload', $config);
				
				if (!$this->upload->do_upload('document')) {
					$params = array(
						'categories' => $this->file->get_categories(),
						'result' => $this->upload->display_errors('<span class="error">', '</span>')
					);
				} else {
					$filename = 'files/studentdocument/'.do_hash($this->userinfo['username'].' - '.$file->name.'.pdf', 'sha1');
					$result = rename($filename.'.pdf', $filename);
					$dbresult = $this->file->update_student_file($id, $this->userinfo['username'], $details);
					if ($result == FALSE) {
						ulink($filename.'.pdf');
						$params = array(
							'categories' => $this->file->get_categories(),
							'result' => '',
							'remarks' => '<span class="error">Dokumen '.$file->name.' gagal diupload. Silakan coba beberapa saat lagi.</span>'
						);
					} else {
						$this->session->set_flashdata('uploadstatus', '<span id="info" class="info">Dokumen '.$file->name.' berhasil diupload.<br /><br /></span>');
						redirect('mahasiswa/profile/document', 'refresh');
						exit;
					}
				}
				
				$this->_load_common_header();
				$this->_load_common_navigation();
				$this->_load_common_sidebar(get_user_sidebar($this->userinfo['role'], 'profile'));
				$this->load->view('mahasiswa/document_upload', $params);
				$this->_load_common_footer();
			} else if ($status == 'Batal') {
				redirect('mahasiswa/profile/document', 'location');
				exit;
			} 
		}
	}
	
	public function delete() {
		if ($this->input->is_ajax_request()) {
			$this->_get_userinfo();
			$id = $this->input->post('id');
			$exists = $this->file->get_category_by_username_and_id($this->userinfo['username'], $id);
			if ($exists == NULL) {
				redirect('mahasiswa/profile/document', 'location');
				exit;
			}
			$name = $exists->name;
			$result = $this->file->delete_student_file($id, $this->userinfo['username'], $name);
			if ($result == TRUE) {
				echo 'Dokumen berhasil dihapus.';
				exit;
			} else {
				echo 'Gagal hapus dokumen. Silakan coba beberapa saat lagi.';
				exit;
			}
		} else {
			redirect('mahasiswa/profile/document', 'location');
			exit;
		}
	}
}